package main.swing;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import main.bean.Email;
import main.inte.Smtp4send;
import main.param.GlobalParam;

public class SendEmail extends JFrame{
	private static JTextField textField;
	private static JTextField textField_1;
	private static JTextArea textArea;
	public SendEmail() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(300, 500);
		getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Subject");
		lblNewLabel.setBounds(10, 20, 54, 15);
		getContentPane().add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(93, 17, 189, 18);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblDestination = new JLabel("Destination");
		lblDestination.setBounds(10, 48, 73, 15);
		getContentPane().add(lblDestination);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(93, 45, 189, 18);
		getContentPane().add(textField_1);
		
		textArea = new JTextArea();
		textArea.setBounds(10, 73, 272, 363);
		getContentPane().add(textArea);
		
		JButton btnNewButton = new JButton("Send");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				SendEmail.sendEmail();
			}
		});
	
		btnNewButton.setBounds(187, 446, 95, 25);
		getContentPane().add(btnNewButton);
		this.setVisible(true);
	}
	protected static void sendEmail() {
		// TODO Auto-generated method stub
		if(textField.getText()==null || textField_1.getText() == null)
			return;
		Email e = new Email();
		e.setSubject(textField.getText().trim());
		e.setFrom(GlobalParam.ACCOUNT);
		e.setReceive(textField_1.getText().trim());
		e.setText(textArea.getText());
		Smtp4send send = new Smtp4send();
		if(send.sendEmail(e)){
			System.out.println("send succeed");
			JOptionPane.showMessageDialog(null,"send OK");
		}
		else {
			JOptionPane.showMessageDialog(null,"send fail");
		}
		
		
	}
	public static void main(String[] args) {
		SendEmail e = new SendEmail();
	}
}
